﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace frmKiemKho
{
    public partial class frmKiemKho : UTLBASEFORM.MDICHILD
    {
        public frmKiemKho()
        {
            InitializeComponent();
        }
        DataTable dttb = new DataTable();
        #region UserFunction
        public static string GetField(string tablename, string GetField, string KeyField, string key)
        {
            string sql = "SELECT * FROM " + tablename + " WHERE " + KeyField + "=" + key;
            UTLDB.UTLDB data = new UTLDB.UTLDB();                      
            DataTable dt = data.GetDataBySQL(sql);
            if (dt.Rows.Count <= 0)
            {
                return "";
            }
            else
            {
                return dt.Rows[0][GetField].ToString();
            }
        }
        private void LoadProduct()
        {
            string sql = "SELECT     ID, Name, Barcode, Status";
            sql += " FROM         tblProduct";
            sql += " WHERE     (Status = 1)";
            UTLDB.UTLDB data = new UTLDB.UTLDB();
            dttb = data.GetDataBySQL(sql);
            this.dtghanghoathat.DataSource = dttb;
            lblSPTrongKho.Text = "Có " + (dtghanghoathat.Rows.Count - 1) + " sản phẩm trong kho";
        }
        private void LoadProduct(int type)
        {
            string sql = "SELECT     ID, Name, Barcode, Status";
            sql += " FROM         tblProduct";
            sql += " WHERE     (Status = 1) AND (IDCat=" + type + ")" ;
            UTLDB.UTLDB data = new UTLDB.UTLDB();
            dttb = data.GetDataBySQL(sql);
            this.dtghanghoathat.DataSource = dttb;
            lblSPTrongKho.Text = "Có " + (dtghanghoathat.Rows.Count - 1) + " sản phẩm trong kho";
        }
        private void LoadProductType()
        {
            string sql="SELECT * FROM tbCatalog";
            UTLDB.UTLDB data = new UTLDB.UTLDB();
            DataTable dt = data.GetDataBySQL(sql);
            this.cboLoaiSP.DataSource = dt;
            this.cboLoaiSP.ValueMember = "ID";
            this.cboLoaiSP.DisplayMember = "Name";
        }
        #endregion
        private void frmKiemKho_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            LoadProduct();
            LoadProductType();
        }

        private void bntLoadFileKiemkho_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();
            if (o.ShowDialog() == DialogResult.OK)
            {
                string c;
                StreamReader taptin = File.OpenText(o.FileName.ToString());
                DataTable dt = new DataTable();
                dt.Columns.Add("date", typeof(string));
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("barcode", typeof(string));
                dt.Columns.Add("Num", typeof(string));
                while (!taptin.EndOfStream)
                {
                    c = taptin.ReadLine().ToString().Trim();
                    string[] temp;
                    temp = c.Split(' ');
                    int[] t ={ 1, 2, 3 };
                    int tt = 0;
                    for (int i = 0; i < temp.Length; i++)
                    {
                        if (temp[i].Length > 0)
                        {
                            t[tt] = i;
                            tt++;
                        }
                    }
                    try
                    {
                        dt.Rows.Add(temp[t[0]], GetField("tblProduct", "Name", "Barcode", "'" + temp[t[1]].ToString() + "'"), temp[t[1]], temp[t[2]]);
                    }
                    catch { }
                }
                taptin.Close();
                this.dtgSanPham.DataSource = dt;
                string sql = "SELECT     ID, Name, Barcode, Status";
                sql += " FROM         tblProduct";
                sql += " WHERE     (Status = 1)";
                UTLDB.UTLDB data = new UTLDB.UTLDB();
                int mat = 0;
                DataTable dtkho = dttb;
                DataTable dtmat = new DataTable();
                dtmat.Columns.Add("Name", typeof(string));
                dtmat.Columns.Add("barcode", typeof(string));
                for (int i = 0; i < dtkho.Rows.Count; i++)
                {
                    Boolean timthay = false;
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (string.Compare(dtkho.Rows[i]["Barcode"].ToString(), dt.Rows[j]["Barcode"].ToString()) == 0)
                        {

                            timthay = true;
                        }
                    }
                    if (timthay == false)
                    {
                        mat++;
                        dtmat.Rows.Add(dtkho.Rows[i]["Name"], dtkho.Rows[i]["Barcode"]);
                    }
                }
                MessageBox.Show("Có " + mat.ToString() + " sản phẩm bị mất");
                dtgSPMat.DataSource = dtmat;
                this.lblSPMat.Text = "Có " + dtmat.Rows.Count + " mặt hàng bị mất";
                this.lblSPTuMayKiemKho.Text = "Có " + dt.Rows.Count + " mặt hàng từ máy kiểm kho";
            }
        }

        private void radioByType_CheckedChanged(object sender, EventArgs e)
        {
            if (radioByType.Checked == true)
            {
                this.cboLoaiSP.Visible = true;
                try
                {
                    LoadProduct(Convert.ToInt32(this.cboLoaiSP.SelectedValue));
                }
                catch { }
            }
        }

        private void radioAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radioAll.Checked == true)
            {
                this.cboLoaiSP.Visible = false;
                LoadProduct();
            }
        }

        private void cboLoaiSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadProduct(Convert.ToInt32(this.cboLoaiSP.SelectedValue));
            }
            catch { }
        }
    }
}